﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.IO;
using System.Security.Permissions;
using System.Diagnostics;
using System.Windows.Controls.Primitives;

namespace SmartWindow
{
    /// <summary>
    /// Interaction logic for SmartApps.xaml
    /// </summary>
    public partial class SmartApps : Window
    {
        #region Variables
        private static List<SmartButton> appList = new List<SmartButton>();
        private static List<Exception> errorsLog = new List<Exception>();
        private double marginLeft;
        private double marginRight;
        private int scrollSpeed = 5;
        #endregion

        #region Code
        public SmartApps(List<SmartButton> links)
        {
            InitializeComponent();
            ResizeMode = ResizeMode.NoResize;
            WindowStyle = WindowStyle.None;
            WindowState = WindowState.Maximized;
            Topmost = true;
            appList = links;
            appList.Sort(delegate(SmartButton sb1, SmartButton sb2) { return sb1.getDateTime().CompareTo(sb2.getDateTime()); });
            //appList.Reverse();

            marginLeft = System.Windows.SystemParameters.PrimaryScreenWidth / 4;
            marginRight = System.Windows.SystemParameters.PrimaryScreenWidth - marginLeft;

            double scroll = SystemParameters.VerticalScrollBarButtonHeight;
            double x = System.Windows.SystemParameters.PrimaryScreenWidth / 3;
            double y = (System.Windows.SystemParameters.PrimaryScreenHeight - scroll) / 3;
            mainScrollViewer.Height = System.Windows.SystemParameters.PrimaryScreenHeight;
            mainScrollViewer.Width = System.Windows.SystemParameters.PrimaryScreenWidth;

            mainGrid.MouseMove += new MouseEventHandler(mainGridMouseMove);

            Color[] colors = { Colors.LightBlue, Colors.LightCyan, Colors.LightGray, Colors.LightGreen, Colors.LightPink, Colors.LightYellow, Colors.LightSalmon, Colors.LightSkyBlue, Colors.Peru };
            int color = 0;
            for (int i = 0; i < appList.Count; i++)
            {
                Button button = new Button();

                UniformGrid uniformGrid = new UniformGrid();
                uniformGrid.Columns = 1;
                uniformGrid.Rows = 3;

                Image image = new Image();
                image.Source = appList[i].getImage().ImageSource;
                TextBlock text = new TextBlock();
                text.Text = appList[i].getAppName();

                uniformGrid.Children.Add(new TextBlock());
                uniformGrid.Children.Add(image);
                uniformGrid.Children.Add(text);

                button.Tag = i;
                button.Width = x;
                button.Height = y;
                button.FontSize = 20;
                button.OpacityMask = new SolidColorBrush(Colors.Black);
                button.FontWeight = FontWeights.SemiBold;
                button.Content = uniformGrid;
                button.Background = new SolidColorBrush(colors[color]);
                button.Click += new RoutedEventHandler(appButtonClick);

                if (color == 8)
                {
                    color = 0;
                }
                else
                {
                    color++;
                }
                mainGrid.Children.Add(button);
            }
        }

        private void mainGridMouseMove(object sender, EventArgs e)
        {
            if (System.Windows.Forms.Cursor.Position.X > marginRight)
            {
                mainGrid.MouseMove += new MouseEventHandler(scrollMoveRight);
            }
            else if (System.Windows.Forms.Cursor.Position.X < this.marginLeft)
            {
                mainGrid.MouseMove += new MouseEventHandler(scrollMoveLeft);
            }
        }

        private void scrollMoveRight(object sender, EventArgs e)
        {
            double x = marginLeft - (System.Windows.SystemParameters.PrimaryScreenWidth - System.Windows.Forms.Cursor.Position.X);
            if (x >= 0)
            {
                double speed = x * scrollSpeed / marginLeft;
                mainScrollViewer.ScrollToHorizontalOffset(mainScrollViewer.HorizontalOffset + speed);
            }
        }

        private void scrollMoveLeft(object sender, EventArgs e)
        {
            double x = System.Windows.Forms.Cursor.Position.X - marginLeft;
            if (x < 0)
            {
                double speed = x * scrollSpeed / marginLeft;
                mainScrollViewer.ScrollToHorizontalOffset(mainScrollViewer.HorizontalOffset + speed);
            }
        }

        private void appButtonClick(object sender, EventArgs e)
        {
            Button button = sender as Button;
            int buttonNumber = Convert.ToInt32(button.Tag);
            try
            {
                if (appList[buttonNumber].getMode() == 4)
                {
                    System.Diagnostics.Process.Start(appList[buttonNumber].getAppPath());
                    System.IO.File.SetLastAccessTime(appList[buttonNumber].getAppPath(), DateTime.Now);
                    appList[buttonNumber].setDateTime(DateTime.Now);
                }
                else if (appList[buttonNumber].getMode() == 1)
                {
                    this.Close();
                }
                /*else if (appList[buttonNumber].getMode() == 2)
                {
                    Process.Start(Environment.CurrentDirectory + EyeSettings.Conventions.POINTER_CALIBRATION);
                }*/
                else if (appList[buttonNumber].getMode() == 3)
                {
                    try
                    {
                        System.Diagnostics.Process.Start(System.Environment.GetEnvironmentVariable("ProgramFiles") + @"\Common Files\microsoft shared\ink\TabTip.exe");
                    }
                    catch (Exception c)
                    {
                        errorsLog.Add(c);
                    }
                    this.Close();
                }
                else if (appList[buttonNumber].getMode() == 0)
                {
                    System.Media.SoundPlayer player = new System.Media.SoundPlayer(appList[buttonNumber].getStream());
                    player.Play();
                }
            }
            catch (Exception c)
            {
                errorsLog.Add(c);
            }
            this.Close();
        }
        #endregion
    }
}
